package com.han.hotelplat.listener;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.FlowStepRuner;
import com.han.hotelplat.comm.area.config.Config;
import com.han.hotelplat.comm.area.config.ResourcesIndex;
import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.comm.utils.FXMD;
import com.han.hotelplat.comm.utils.IOHelp;
import com.han.hotelplat.comm.utils.JsonP;
import com.han.hotelplat.comm.utils.ResourceHelp;
import com.han.hotelplat.model.AreaModel;
import com.han.hotelplat.pojo.employee.Employee;


/**
 * 绽放的花朵🌺 启动时的流程
 * @author lkm
 * @version Created on 2017年10月16日 上午10:50:04
 */
@Component
public class SysBootFlower implements ApplicationListener<ContextRefreshedEvent>{
	@Autowired
	private EntityManage entityManage;
	
	static {
		//配置时区
		TimeZone.setDefault(Config.timeZone);
		//配置本地化
		Locale.setDefault(Locale.Category.FORMAT, Locale.CHINESE);
		
	}
	
	@Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
		setAreaModel();
		loadAllBank();
		loadAllHoliday();
    }

	/*private void loadAllCity() {
		AreaModel [] allProvince = AreaHelp.getInstance().getChinaProvinces();
		Constant.cityCodeMap = new HashMap<String, String>();
		Constant.cityNameMap = new
		for(AreaModel a : allProvince){
			Constant.cityCodeMap.put(a.getCode(), a.getName());
			Constant.cityNameMap.put(a.getName(), a.getCode());
		}
	}*/

	private void loadAllHoliday() {
		String sql = "select id, holidaydate from tb_holiday";
		List<Object[]> data = entityManage.findBySql(sql);
		Map<String, String> _holidayMap = new HashMap<String, String>();
		for(Object [] obj: data){
			String date = DateUtil.formatYMD((Date)obj[1]);
			_holidayMap.put(date, date);
		}
		Constant.holidayMap = _holidayMap;
	}

	private void loadAllBank() {
		String sql = "select bankcode, bankname from tb_bank";
		List<String[]> data = entityManage.findStringDataBySql(sql);
		Map<Integer, String> bankMap = new HashMap<Integer, String>();
		for(String [] obj: data){
			bankMap.put(Integer.parseInt(obj[0]), obj[1]);
		}
		Constant.bandMap = bankMap;
	}

	public static void setAreaModel(){
		AreaModel gAreaModel;
		{
			JsonP jsonP=null;
			try {
				 jsonP=new JsonP(IOHelp.parserInputStreamToString(
						 new FileInputStream(ResourceHelp.getResourceFile(ResourcesIndex.Address_json)), "utf-8").toString()
						 );
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException("加载  Addressjson 失败");
			}
			

			//得到国家的。中国家是100000
			JsonP gP=jsonP.get("districts").get(0);
			gAreaModel=new AreaModel(gP.get("adcode").getAsString(null), 
					gP.get("name").getAsString(null)
					, (byte)0, null, null);
			
			//get("districts") 省份列表34个
			List<AreaModel> pAreaModels=new ArrayList<AreaModel>();
			JsonP provincesJsonP= gP.get("districts");
			//循环省
			for (int i = 0; i < Integer.MAX_VALUE; i++) {
				JsonP pj = provincesJsonP.get(i);
				if(pj==null) break;
			
				AreaModel pAreaModel=new AreaModel(pj.get("adcode").getAsString(null), 
						pj.get("name").getAsString(null)
						, (byte)1, null, null);
				
				List<AreaModel> cAreaModels=new ArrayList<AreaModel>();
				//城市列表
				JsonP csj=pj.get("districts");
				for (int j = 0; j < Integer.MAX_VALUE; j++) {
					//城市
					JsonP cj = csj.get(j);
					if(cj==null)break;
					
					AreaModel cAreaModel=new AreaModel(cj.get("adcode").getAsString(null), 
							cj.get("name").getAsString(null)
							, (byte)2, pAreaModel, null);
					
					List<AreaModel> dAreaModels=new ArrayList<AreaModel>();
					//区列表
					JsonP dsj = cj.get("districts");
					for (int k = 0; k < Integer.MAX_VALUE; k++) {
						JsonP dj = dsj.get(k);
						if(dj==null)break;
						AreaModel dAreaModel=new AreaModel(dj.get("adcode").getAsString(null), 
								dj.get("name").getAsString(null)
								, (byte)3, cAreaModel, null);
						dAreaModels.add(dAreaModel);
					}
					//区列表 end
					//城市添加孩子。就是区域
					cAreaModel.setChilds(dAreaModels.toArray(new AreaModel[dAreaModels.size()]));
					cAreaModels.add(cAreaModel);
				}
				//城市列表  end
				pAreaModel.setChilds(cAreaModels.toArray(new AreaModel[cAreaModels.size()]));
				pAreaModels.add(pAreaModel);
			}
			gAreaModel.setChilds(pAreaModels.toArray(new AreaModel[pAreaModels.size()]));
		}
		
		Map<String, AreaModel>  areaModelIndex= new HashMap<String, AreaModel>();

		AreaModel[] pAreaModels=gAreaModel.getChilds();
		//城市
		for (AreaModel pm : pAreaModels) {
			areaModelIndex.put(pm.getCode(), pm);
			AreaModel[] cms=pm.getChilds();
//			城市列表
			for (AreaModel cm : cms) {
				areaModelIndex.put(cm.getCode(), cm);
				AreaModel[] dms=cm.getChilds();
				for (AreaModel dm : dms) {
					areaModelIndex.put(dm.getCode(), dm);
				}
			}
		}
		areaModelIndex.put(gAreaModel.getCode(),gAreaModel);
		AreaHelp.getInstance().set(areaModelIndex);
	}

}
